package ig.ejb.metrica.da.grupo;


import java.sql.SQLException;
import java.lang.Exception;
import ig.da.abs.AbstractBasicDao;
import ig.ejb.metrica.da.grupo.GrupoorigendatoDTO;

/**
 * GenJ 2011
 * @author usuario
 *
 * Is an Class for table DB for DAO
 */
public class GrupoorigendatoDAO extends AbstractBasicDao{
//ss
	// -----------------------------------------------------------------
	// Constants
	// -----------------------------------------------------------------

	// -----------------------------------------------------------------
	// Attributes
	// -----------------------------------------------------------------
    private java.sql.Connection connection = null;

	// -----------------------------------------------------------------
	// Constructors
	// -----------------------------------------------------------------
    public GrupoorigendatoDAO ()  {

    }

	// -----------------------------------------------------------------
	// All Methods
	// -----------------------------------------------------------------

                

    /**
     * Method for create an record of table of table
     * @return : the pk of table
     * @param dto
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public  Integer  create(GrupoorigendatoDTO dto) throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;

        try{
            String QUERY = "INSERT INTO GRUPOORIGENDATO ( ID, NOMBRE ) VALUES ( ?,  ? )";
            if(dto.getPk()==null){
                stat = connection.createStatement();
                res = stat.executeQuery("SELECT MAX(ID) FROM GRUPOORIGENDATO");
                if(res.next()){
                    Integer pk = res.getInt (1);
                    dto.setPk(pk !=null ? pk + 1 : 1 );
                }
            }
            prep = connection.prepareStatement(QUERY);
            prep.setInt (1,dto.getId());
            prep.setString (2,dto.getNombre());
            prep.executeUpdate();
        } catch (SQLException ex) {
            dto.setPk( null );
            throw ex;
        } catch (Exception ex) {
            dto.setPk( null );
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return dto.getPk();
    }


    /**
     * Update by Pk
     * @param dto
     * @return teh number record updates
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int update (GrupoorigendatoDTO dto) throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        connection = open();
        try{
            String QUERY = "UPDATE GRUPOORIGENDATO SET NOMBRE = ? WHERE ID = ?";
            prep = connection.prepareStatement(QUERY);
            prep.setString (1,dto.getNombre());

            prep.setInt (2,dto.getId());
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Delete by Pk
     * @param pk
     * @return the number record delete
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int delete (GrupoorigendatoDTO dto ) throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        connection = open();

        try{
            String QUERY = "DELETE FROM GRUPOORIGENDATO WHERE ID = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,dto .getId());
            
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Find by Pk
     * @param pk
     * @return DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public GrupoorigendatoDTO findByPk (GrupoorigendatoDTO dto ) throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        GrupoorigendatoDTO grupoorigendato = null;
        try{
            String QUERY = "SELECT ID, NOMBRE FROM GRUPOORIGENDATO WHERE ID = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt (1,dto .getId());
            res = prep.executeQuery();
            if (res.next()) {
                grupoorigendato = new GrupoorigendatoDTO(  res.getInt ( 1 ), res.getString ( 2 ));
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,res,connection);
        }
        return grupoorigendato;
    }

    /**
     * Find by All
     * @return the list DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public java.util.List<GrupoorigendatoDTO> findAll() throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<GrupoorigendatoDTO> list=new java.util.ArrayList<GrupoorigendatoDTO>();

        try{
            String QUERY = "SELECT ID, NOMBRE FROM GRUPOORIGENDATO ";
            prep = connection.prepareStatement( QUERY );
            res = prep.executeQuery();

            while (res.next()) {
                list.add(new GrupoorigendatoDTO(  res.getInt ( 1 ), res.getString ( 2 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    public java.util.List< Integer > createAll(java.util.List<GrupoorigendatoDTO> dtos) throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;
        java.util.List< Integer > list=new java.util.ArrayList< Integer >();
        try{
            String QUERY = "INSERT INTO GRUPOORIGENDATO ( ID, NOMBRE ) VALUES ( ?,  ? )";
            for(GrupoorigendatoDTO dto : dtos ){
                if(dto.getPk()==null){
                    stat = connection.createStatement();
                    res = stat.executeQuery("SELECT MAX(ID) FROM GRUPOORIGENDATO");
                    if(res.next()){
                        Integer pk = res.getInt (1);
                        dto.setPk(pk !=null ? pk + 1 : 1 );
                    }
                }
                prep = connection.prepareStatement(QUERY);
                prep.setInt (1,dto.getId());
                prep.setString (2,dto.getNombre());
                prep.executeUpdate();
                list.add(dto.getPk());
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return list;
    }






    /**
     * Open the connection
     * @return
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    private java.sql.Connection open() throws ClassNotFoundException, java.sql.SQLException,Exception {
    	return getConnection("METRICADW");
    }



}
